#!/bin/bash
# 设置 Notepad-- 为系统默认文本编辑器
# 适用于 Linux (GNOME, KDE, XFCE等) 和 macOS

set -e

echo "============================================"
echo "设置 Notepad-- 为系统默认编辑器"
echo "Set Notepad-- as Default Text Editor"
echo "============================================"
echo ""

# 检测操作系统
OS_TYPE=$(uname -s)

if [ "$OS_TYPE" = "Darwin" ]; then
    # macOS
    echo "检测到 macOS 系统 / Detected macOS"
    echo ""
    echo "请执行以下步骤 / Please follow these steps:"
    echo ""
    echo "1. 右键点击任意文本文件"
    echo "   Right-click any text file"
    echo ""
    echo "2. 选择 '显示简介' 或 'Get Info'"
    echo ""
    echo "3. 在 '打开方式' 中选择 Notepad--"
    echo "   In 'Open with', select Notepad--"
    echo ""
    echo "4. 点击 '全部更改...' 按钮"
    echo "   Click 'Change All...' button"
    echo ""
    echo "或使用命令行（需要应用已安装）："
    echo "Or use command line (app must be installed):"
    echo ""
    echo "  duti -s io.gitee.cxasm.notepad-- .txt all"
    echo "  duti -s io.gitee.cxasm.notepad-- .md all"
    echo ""
    
elif [ "$OS_TYPE" = "Linux" ]; then
    # Linux
    echo "检测到 Linux 系统 / Detected Linux"
    echo ""
    
    # 检测桌面环境
    if [ -n "$XDG_CURRENT_DESKTOP" ]; then
        echo "桌面环境 / Desktop: $XDG_CURRENT_DESKTOP"
    fi
    
    echo ""
    echo "设置默认编辑器... / Setting default editor..."
    echo ""
    
    # 方法1: xdg-mime (通用方法)
    if command -v xdg-mime >/dev/null 2>&1; then
        echo "使用 xdg-mime 设置默认关联..."
        
        # 常见文本类型
        TEXT_TYPES=(
            "text/plain"
            "text/x-chdr"
            "text/x-csrc"
            "text/x-c++hdr"
            "text/x-c++src"
            "text/x-java"
            "text/x-python"
            "application/x-shellscript"
            "text/x-sh"
            "text/html"
            "text/css"
            "text/xml"
            "application/xml"
            "application/json"
            "text/markdown"
            "text/x-log"
        )
        
        for mime_type in "${TEXT_TYPES[@]}"; do
            xdg-mime default notepad--.desktop "$mime_type" 2>/dev/null || true
            echo "  ✓ $mime_type"
        done
        
        echo ""
        echo "✓ 已设置为默认文本编辑器"
        echo "✓ Set as default text editor"
    else
        echo "⚠ xdg-mime 未找到，请手动设置"
        echo "⚠ xdg-mime not found, please set manually"
    fi
    
    # 方法2: update-desktop-database (更新桌面数据库)
    if command -v update-desktop-database >/dev/null 2>&1; then
        echo ""
        echo "更新桌面数据库... / Updating desktop database..."
        if [ -w "/usr/share/applications" ]; then
            update-desktop-database /usr/share/applications 2>/dev/null || true
        fi
        if [ -w "$HOME/.local/share/applications" ]; then
            update-desktop-database "$HOME/.local/share/applications" 2>/dev/null || true
        fi
        echo "✓ 完成"
    fi
    
    # 方法3: update-mime-database (更新MIME数据库)
    if command -v update-mime-database >/dev/null 2>&1; then
        echo ""
        echo "更新 MIME 数据库... / Updating MIME database..."
        if [ -w "/usr/share/mime" ]; then
            update-mime-database /usr/share/mime 2>/dev/null || true
        fi
        if [ -w "$HOME/.local/share/mime" ]; then
            update-mime-database "$HOME/.local/share/mime" 2>/dev/null || true
        fi
        echo "✓ 完成"
    fi
    
    echo ""
    echo "============================================"
    echo "设置完成！/ Setup complete!"
    echo "============================================"
    echo ""
    echo "您现在可以右键点击文本文件，"
    echo "选择 '打开方式' 查看 Notepad-- 是否为默认编辑器"
    echo ""
    echo "You can now right-click text files and check"
    echo "if Notepad-- is the default editor"
    echo ""
    
else
    echo "不支持的操作系统: $OS_TYPE"
    echo "Unsupported OS: $OS_TYPE"
    exit 1
fi

